'use strict';
const Controller = require('egg').Controller;
class AgreementCpntroller extends Controller{
    
    async index() { //搜索
        const {ctx,app} = this;
        const {keyname} = ctx.request.query
        let where=''
        if(keyname){
            where=`where agr_title like "%${keyname}%"`
        }
        const result =await app.mysql.query(`select * from agreement ${where}`)
        ctx.body = {
            code:0,
            msg:"查找成功",
            data:result
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('agreement',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
     //改
     async update() {//put
        const {ctx,app} =this;
        const {agr_title,agr_content} = ctx.request.body
        const { id } = ctx.params;
        const row={
            id,
            agr_title,
            agr_content,
            agr_time:Date.now()
        };
        
        const result=await app.mysql.update('agreement',row);
            if(result){
                ctx.body={
                    code:0,
                    msg:"编辑成功",
                    row,
                };
            }else{
                ctx.body={
                    code:4008,
                    msg:"编辑失败",
                };
            } 
    }
}
module.exports = AgreementCpntroller